﻿using System;
using System.Collections.Generic;
using System.Text;
using FE.Common;
using FE.Service.Contract;
using FE.Shared;
using FE.Shared.Entity;
using FE.Shared.Enum;
using Newtonsoft.Json;
using SqlSugar;

namespace FE.Service.Repository.SqlSugar
{
    public class MySqlDbContext : IMySqlDbContext
    {
        private readonly IConfigurationService _configurationService;
        //private readonly ILogger _logger;

        public MySqlDbContext(IConfigurationService configurationService
            //, ILogger logger
            )
        {
            _configurationService = configurationService;
            //_logger = logger;
        }

        public bool TestConnection()
        {
            var db = GetSugarClient();

            var query = db.Queryable<Sys_Attachment>().Where(i => i.SA00 > 0);

            return query.Count() > 0;
        }

        private SqlSugarClient GetSugarClient()
        {
            var jsonText = FileHelper.ReadAllText(_configurationService.LocalJsonFilePath);
            var obj = JsonConvert.DeserializeObject<AppSettings>(jsonText);
            var connStr = AppSettingsTool.GetConnectionString(obj);

            var db = new SqlSugarClient(new ConnectionConfig()
            { ConnectionString = connStr, DbType = DbType.MySql, IsAutoCloseConnection = true });

            //每次Sql执行前事件
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                //我可以在这里面写逻辑
                Console.WriteLine(sql);
                Console.WriteLine(pars);
            };

            return db;
        }

        public int UpdateAttachmentStatus(int attachmentId, AttachmentStatus status, string msg)
        {
            var entity = new AttachmentStatusDto()
            {
                SA00 = attachmentId,
                Pdf_Generate_Flag = status,
                Pdf_Generate_Msg = msg
            };

            return UpdateAttachmentStatus(entity);
        }

        private int UpdateAttachmentStatus(AttachmentStatusDto attachStatus)
        {
            int result = 0;

            var db = GetSugarClient();

            result = db.Updateable<Sys_Attachment>()
                .SetColumns(it => new Sys_Attachment()
                {
                    Pdf_Generate_Flag = attachStatus.Pdf_Generate_Flag,
                    Pdf_Generate_Time = attachStatus.Pdf_Generate_Time,
                    Pdf_Generate_Msg = attachStatus.Pdf_Generate_Msg
                })
                .Where(it => it.SA00 == attachStatus.SA00)
                .ExecuteCommand();

            return result;
        }

        public ISugarQueryable<Sys_Attachment> GetAttachmentList()
        {
            //todo 一次循环只做50个文件
            //是附件,且时间小于2天内的 SA05
            var db = GetSugarClient();

            var result = db.Queryable<Sys_Attachment>()
                .Where(
                    //类型为附件 
                    it => it.SA08 == "0"
                          //未生成pdf的记录
                          && (it.Pdf_Generate_Flag == null || it.Pdf_Generate_Flag == AttachmentStatus.Normal)
                          //2日内
                          && (DateTime.Now.AddDays(-2) <= it.SA05 || it.SA05 <= DateTime.Now));


            return result;
        }
    }
}
